#include "s5p4418_gpio.h"
void* GpioBase = NULL;
int fd=-1;
int gpio_init()
{
	fd = open("/dev/mem", O_RDWR | O_SYNC);
	if(fd<0)
	{
		return ERR_FILEERR;
	}
	GpioBase = mmap(NULL, (GPIO_EndAddr-GPIO_StartAddr), PROT_READ|PROT_WRITE, MAP_SHARED,fd, GPIO_StartAddr);
	if(GpioBase == NULL || GpioBase == MAP_FAILED)
	{
            gpio_exit();
            return ERR_GMAPERR;
	}
	return 0;
}

void gpio_exit(){
	if(GpioBase != NULL)
	{
		munmap(GpioBase,(GPIO_EndAddr-GPIO_StartAddr));
		GpioBase = NULL;
	}
	if(fd != -1)
	{
		close(fd);
		fd = -1;
	}
	return;
}

void gpio_ioctl(unsigned int blank, int num, int type)
{
	unsigned int tmp=0;
	tmp = REG((unsigned int)GpioBase + blank + 4);
	tmp &= ~(1<<num);
	tmp |= (type << num);
	REG((unsigned int)GpioBase + blank + 4) = tmp;
}

unsigned int gpio_readctl(unsigned int blank, int num)
{
	unsigned int tmp=0;
    tmp=REG((unsigned int)GpioBase +blank+4);
    tmp &= (1<<num);
    return tmp?1:0;
}

unsigned int gpio_read(unsigned int blank, int num)
{
	unsigned int tmp=0;
    tmp=REG((unsigned int)GpioBase +blank + 0x014);
    tmp &= (1<<num);
    return tmp?1:0;
}

void gpio_write(unsigned int blank, int num, int status)
{
	unsigned int tmp=0;
    tmp=REG((unsigned int)GpioBase +blank);
    tmp &= ~(1<<num);
    tmp |= (!!status<<num);
    REG((unsigned int)GpioBase +blank)=tmp;
}